package com.smbms.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smbms.dao.BillMapper;
import com.smbms.pojo.Bill;
import com.smbms.util.BillPage;
import com.smbms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class BillServiceImpl implements BillService {

    @Autowired  //注入属性，在spring容器中找到BillMapper的实现类进行注入
    BillMapper billMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public int addBill(Bill bill) {
        Set<String> keys = redisTemplate.keys("bill*");
        for (String key : keys) {
            redisTemplate.delete(key);
        }
        return billMapper.addBill(bill);
    }

    @Override
    public Bill checkByBillCodeAdd(String billCode) {
        Bill bill;
        String key = "bill.checkByBillCodeAdd.billCode=" + billCode;
        if(redisTemplate.hasKey(key)){
            bill = (Bill) redisTemplate.opsForValue().get(key);
        }else {
            bill = billMapper.checkByBillCodeAdd(billCode);
            redisTemplate.opsForValue().set(key, bill);
        }
        return bill;
    }

    @Override
    public int delBill(Integer id) {
        Set<String> keys = redisTemplate.keys("bill*");
        for (String key : keys) {
            redisTemplate.delete(key);
        }
        return billMapper.delBill(id);
    }

    @Override
    public Bill findById(Integer id) {
        Bill bill;
        String key = "bill.findById.id=" + id;
        if(redisTemplate.hasKey(key)){
            bill = (Bill) redisTemplate.opsForValue().get(key);
        }else {
            bill = billMapper.findById(id);
            redisTemplate.opsForValue().set(key, bill);
        }
        return bill;
    }

    @Override
    public int checkByBillCodeUpd(String billCode, String self) {
        int flag;
        String key = "bill.checkByBillCodeUpd.billCode=" + billCode + "&self=" + self;
        if(redisTemplate.hasKey(key)){
            flag = (int) redisTemplate.opsForValue().get(key);
        }else{
            flag = billMapper.checkByBillCodeUpd(billCode, self);
            redisTemplate.opsForValue().set(key,flag);
        }
        return flag;
    }

    @Override
    public int updBill(Bill bill) {
        Set<String> keys = redisTemplate.keys("bill*");
        for (String key : keys) {
            redisTemplate.delete(key);
        }
        return billMapper.updBill(bill);
    }

    @Override
    public PageResult findPage(BillPage billPage) {
        PageResult pageResult;
        String key = "bill.findPage.productName=" + billPage.getProductName() + "&providerId=" + billPage.getProviderId()
                + "&isPayment=" + billPage.getIsPayment() + "&page=" + billPage.getPage() + "&size=" + billPage.getSize();
        if(redisTemplate.hasKey(key)){
            pageResult = (PageResult) redisTemplate.opsForValue().get(key);
        }else {
            PageHelper.startPage(billPage.getPage(),billPage.getSize());
            Page<Bill> bills = billMapper.findPage(billPage);
            //获取列表
            List<Bill> billList = bills.getResult();
            //获取数量
            long total = bills.getTotal();
            pageResult = new PageResult(total,billList);
            //保存到redis缓存中
            redisTemplate.opsForValue().set(key,pageResult);
        }
        return pageResult;
    }
}
